/*
 * Copyright ©2021 su binglun(9085309@qq.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *---------------------------------------------------------------------
 *-                                                                   -
 *-           Website : http://phoenix.sunny360.cn/                   -
 *-                                                                   -
 *---------------------------------------------------------------------
 */
package cn.sunny360.core.handler.http;

import cn.sunny360.core.service.LinkmanService;
import cn.sunny360.core.utils.Container;
import io.vertx.core.Promise;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import org.apache.commons.lang3.StringUtils;

public class LinkmanHandler extends HttpBaseHandler {

	private static final Logger logger = LoggerFactory.getLogger(LinkmanHandler.class);

	private LinkmanService linkmanService;

	public LinkmanHandler() {
		this.linkmanService = Container.getComponent(LinkmanService.class);
	}

	public void getUserLinkmanList(RoutingContext context) {
		String contentType = context.request().getHeader("Content-Type");
		if (StringUtils.isNotBlank(contentType) && contentType.contains("application/octet-stream")) {
			JsonObject body = context.getBodyAsJson();
			String userId = body.getString("user_id");
			// 查询用户联系人列表
			Promise<JsonArray> promise = Promise.promise();
			linkmanService.getLinkmanList(Long.parseLong(userId), promise);
			promiseHandlerResult(context, promise);

		} else {
			logger.info("查询用户列表，请求数据格式错误");
			badResult(context, new Exception("查询用户列表，请求数据格式错误"));
		}
	}

	public void addUserLinkman(RoutingContext context) {
		String contentType = context.request().getHeader("Content-Type");
		if (StringUtils.isNotBlank(contentType) && contentType.contains("application/octet-stream")) {
			JsonObject body = context.getBodyAsJson();
			try {
				Long userId = body.getLong("userId");
				Long linkmanId = body.getLong("linkmanId");

				// 添加联系人
				Promise<String> promise = Promise.promise();
				linkmanService.addLinkman(userId, linkmanId, promise);
				promiseHandlerResult(context, promise);

			} catch (Exception e) {
				e.printStackTrace();
				logger.error("", e);
				badResult(context, e);
			}
		} else {
			logger.info("添加联系人，请求数据格式错误");
			badResult(context, new Exception("添加联系人，请求数据格式错误"));
		}
	}

	public void removeUserLinkman(RoutingContext context) {
		String contentType = context.request().getHeader("Content-Type");
		if (StringUtils.isNotBlank(contentType)
				&& contentType.contains("application/octet-stream")) {

			JsonObject body = context.getBodyAsJson();
			Long userId = body.getLong("userId");
			Long linkmanId = body.getLong("linkmanId");

			// 删除联系人
			Promise<String> promise = Promise.promise();
			linkmanService.removeLinkman(userId, linkmanId, promise);
			promiseHandlerResult(context, promise);

		} else {
			logger.info("删除联系人，请求数据格式错误");
			badResult(context, "删除联系人，请求数据格式错误");
		}
	}

	public void updateUserLinkTime(RoutingContext context) {
		String contentType = context.request().getHeader("Content-Type");
		if (StringUtils.isNotBlank(contentType)
				&& contentType.contains("application/octet-stream")) {

			JsonObject body = context.getBodyAsJson();
			Long userId = body.getLong("userId");
			Long linkmanId = body.getLong("linkmanId");

			// 更新联系人联系时间
			Promise<String> promise = Promise.promise();
			linkmanService.updateLinkTime(userId, linkmanId, promise);
			promiseHandlerResult(context, promise);

		} else {
			logger.info("更新联系人联系时间，请求数据格式错误");
			badResult(context, "更新联系人联系时间，请求数据格式错误");
		}
	}


}
